package typeErasure;


import com.sun.tools.javac.jvm.Gen;

import java.util.*;

public class GenericTest {

    public static void main(String[] args) {

        List<String> la = new ArrayList<>();
        List<Integer> lb = new ArrayList<>();
        System.out.println(la.getClass());
        System.out.println(la.getClass() == lb.getClass());

        GenericType<String> test1 = new GenericType<>();
        GenericType<Integer> test2 = new GenericType<>();

        /**
         * 泛型类型的实际参数是String，
         * 传递给泛型方法的类型是Integer
         * 两者无关
         */
        GenericType2<Integer> t = new GenericType2<>();
        t.testMethod("generic");

        Integer i = t.testMethod2(new Integer(1));
        System.out.println(i);


        GenericInterface<String> genericInterface = new GenericInterfaceImpl<>();
        System.out.println(
                genericInterface.getType("a")
        );


        // 通配符泛型

        Sub sub = new Sub();
        Base base = sub;


        List<Sub> subList = new ArrayList<>();
        subList.add(new Sub());
//        List<Base> baseList = subList;  incompatible type

        // 通配符的出现是为了置顶泛型中类型的范围
        // <?> 被称作无现在订的通配符
        // <? extends T> 被称作有上限通配符
        // <? super T> 被称作有下限通配符

        List<?> list = new ArrayList<>();
//        list.add(123); 无法通过编译

        GenericType4.test2(subList);

        // 泛型的类型参数只能类类型（包括自定义类）， 不能是基本类型
        // 传入的实参与泛型的类型参数相同， 即为Integer
        GenericType<Integer> genericInteger = new GenericType<Integer>(12345);

        GenericType<String> genericString = new GenericType<String>("key_value");
        System.out.println("Generic test, key is:" + genericInteger.field1);
        System.out.println("Generic test, key is:" + genericString.field1);

        // 不传入泛型实参
        GenericType genericType = new GenericType("11111");
        GenericType genericType1 = new GenericType(44444);
        GenericType genericType2 = new GenericType(55.55);
        GenericType genericType3 = new GenericType(false);

        System.out.println("Generic test, key is:" + genericType.field1);
        System.out.println("Generic test, key is:" + genericType1.field1);
        System.out.println("Generic test, key is:" + genericType2.field1);
        System.out.println("Generic test, key is:" + genericType3.field1);


        List<Integer> list1 = new ArrayList<Integer>();
        Map<Integer, String> map = new HashMap<Integer, String>();
        System.out.println(Arrays.toString(list1.getClass().getTypeParameters()));
        System.out.println(Arrays.toString(map.getClass().getTypeParameters()));





    }

}
